package cn.pdx.elec.web.action;


import java.util.Hashtable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.pdx.elec.container.ServiceProvider;
import cn.pdx.elec.domain.ElecUser;
import cn.pdx.elec.service.ElecCommonMsgService;
import cn.pdx.elec.service.ElecLogService;
import cn.pdx.elec.service.ElecUserService;
import cn.pdx.elec.util.LoginUtils;
import cn.pdx.elec.util.MD5keyBean;
import cn.pdx.elec.web.form.ElecCommonMsgForm;
import cn.pdx.elec.web.form.ElecLoginForm;
import com.opensymphony.xwork2.ModelDriven;

/**
 * implements ModelDriven<T>实现这个接口，泛型
 * ModelDriven<ElecTextForm>表示这个表单对象
 * @author 彭定旭
 *@Create Date:2013-3-27(创建日期)
 */
@SuppressWarnings("serial")
public class ElecLoginAction extends BaseAction implements  ModelDriven<ElecLoginForm>{
    
	
	 /**
	 * 引入代办事宜业务接口，注入action
	 */
	 @SuppressWarnings("unused")
	private ElecCommonMsgService elecCommonMsgService=(ElecCommonMsgService) ServiceProvider.getService(ElecCommonMsgService.SERVICE_NAME);
	
	
	 @SuppressWarnings("unused")
	private ElecUserService elecUserService=(ElecUserService) ServiceProvider.getService(ElecUserService.SERVICE_NAME);
	/**
	 * 创建一个表单对象，对应页面的属性的对象
	 */
      private ElecLoginForm elecLoginForm=new ElecLoginForm();
	
      //使用log4j
		Log log= LogFactory.getLog(ElecLoginAction.class);
		
		/**
		 * 调用日志管理业务接口
		 */
		private ElecLogService elecLogService=(ElecLogService) ServiceProvider.getService(ElecLogService.SERVICE_NAME);
		
	public ElecLoginForm getModel() {
		
		return elecLoginForm;
	}
   
	/**
	 * @Name:home
	 * @Description:主页面//从登陆页面获取登录名和密码,验证是否合法
	 *                     如果合法，则验证成功，跳转到home.jsp
	 *                     如果不合法，验证失败，回退到index.jsp(修改时间2013-4-21)
	 * @author 彭定旭
	 * @Create Date:2013-3-27(创建日期)(修改时间2013-4-21)
	 * @return home,成功->home.jsp    不成功->index.jsp
	 */
	public String home() throws Exception
	{
		
		/**
		 * @create 2013-4-24
		 * 添加验证码的校验功能,先校验验证码,在用户名和密码,给request对象,从session中获取
		 */
		boolean flag=LoginUtils.checkNumber(request);
		
		if(!flag){
			
			this.addFieldError("error", "验证码为空或者输入有误！");
			
			return "error";
		}
		
		//获取当前用户名和密码
		String name=elecLoginForm.getName();
		
		String password=elecLoginForm.getPassword();
		
		/**
		 * 使用MD5加密密码先,在和数据库加密的对比
		 */
		MD5keyBean md5=new MD5keyBean();
		String md5password=md5.getkeyBeanofStr(password);
		
		//使用登录名查询用户；获取用户的详细信息
		ElecUser elecUser=elecUserService.findElecUserByLogonName(name);
		
		if(elecUser==null)
		{
			//返回index.jsp
			this.addFieldError("error", "您当前输入的登录名不存在");
			return "error";
		}
		/**
		 * 判断密码
		 */
		if(password==null || password.equals("") || !elecUser.getLogonPwd().equals(md5password))
		{
			this.addFieldError("error", "你当前输入的密码有误或者不存在！");
			return "error";
		}
		request.getSession().setAttribute("globle_user", elecUser);
		
		//获取当前登录名所具有的权限
		String popedom=elecUserService.findElecPopedomByLogonName(name);
		System.out.println("999999999999999="+popedom);
		if(popedom==null || "".equals(popedom))
		{
			this.addFieldError("error", "当前的用户没有分配权限,请与管理员联系！");
			return "error";
		}
		request.getSession().setAttribute("globle_popedom", popedom);
		
		//获取当前登录名所具有的角色
		Hashtable<String,String> hashtable=elecUserService.findeElecRoleByLogonName(name);
		
		if(hashtable==null)
		{
			this.addFieldError("error", "当前的用户没有分配角色,请与管理员联系！");
			return "error";
			
		}
		request.getSession().setAttribute("globle_role", hashtable);
		
		
		/**
		 * @create 2013-4-24
		 * 添加cookie,记住我的功能,放在最后,一切搞定之后在放入
		 */
		LoginUtils.remeberMeByCookie(request,response);
		
		//添加日志管理--2014-3-24维护系统性能安全,begin
		
		/*java.sql.Date date=new java.sql.Date(new java.util.Date().getTime());
		String d=date.toString();
		log.info("用户名为：【"+elecUser.getUserName()+"】登录系统！时间是："+d );*/
		
		/**
		 * 使用日志模块,写入数据库
		 */
		elecLogService.saveElecLog(request,"登录模块：当前用户【"+elecUser.getUserName()+"】登录系统！");
		
		return "home";
		
	}
	
	/**
	 * @Description:logout 重新登录首页
	 * @author 彭定旭
	 * @Create Date:2013-4-24(创建日期)
	 * @return
	 * @throws Exception
	 */
	public String logout() throws Exception
	{
		//清空所有session
		request.getSession().invalidate();
		
		return "logout";
		
	}
	
	/**
	 * @Description:tite,头部页面
	 * @author 彭定旭
	 * @Create Date:2013-3-27(创建日期)
	 * @return
	 * @throws Exception
	 */
	public String title() throws Exception
	{
		return "title";
		
	}
	/**
	 * @Description:left,左页面
	 * @author 彭定旭
	 * @Create Date:2013-3-27(创建日期)
	 * @return
	 * @throws Exception
	 */
	public String left() throws Exception
	{
		return "left";
		
	}
	/**
	 * @Description:change1,改变全屏半屏
	 * @author 彭定旭
	 * @Create Date:2013-3-27(创建日期)
	 * @return
	 * @throws Exception
	 */
	public String change1() throws Exception
	{
		return "change1";
		
	}
	
	/**
	 * @Description:loading,加载首页不同板块信息
	 * @author 彭定旭
	 * @Create Date:2013-3-27(创建日期)
	 * @return
	 * @throws Exception
	 */
	public String loading() throws Exception
	{
		return "loading";
		
	}
	
	/**
	 *@Description: alermXZ,loading页面中的校准提醒模块
	 * @author 彭定旭
	 * @Create Date:2013-3-27(创建日期)
	 * @return
	 * @throws Exception
	 */
	public String alermXZ() throws Exception
	{
		return "alermXZ";
		
	}
	
	/**
	 *@Description: alermJX,loading页面中的检修提醒模块
	 * @author 彭定旭
	 * @Create Date:2013-3-27(创建日期)
	 * @return
	 * @throws Exception
	 */
	public String alermJX() throws Exception
	{
		return "alermJX";
		
	}
	
	/**
	 *@Description: alermZD,loading页面中的站点运行情况模块,查询站点的当天的运行情况
	 * @author 彭定旭
	 * @Create Date:2013-3-27,28(创建日期)
	 * @return alermZD 跳转到alermZD.jsp
	 * @throws Exception
	 */
	public String alermZD() throws Exception
	{  
		List<ElecCommonMsgForm> list=elecCommonMsgService.findElecCommonMsgListByCurrentDate();
		request.setAttribute("commList", list);
		return "alermZD";
		
	}
	
	/**
	 *@Description: alermSB,loading页面中的站点运行情况模块,查询站点的当天的运行情况
	 * @author 彭定旭
	 * @Create Date:2013-3-27,28(创建日期)
	 * @return alermSB 跳转到alermSB.jsp
	 * @throws Exception
	 */
	public String alermSB() throws Exception
	{
		List<ElecCommonMsgForm> list=elecCommonMsgService.findElecCommonMsgListByCurrentDate();
		request.setAttribute("commList", list);
		return "alermSB";
		
	}
	
	/**
	 *@Description: alermYS,loading页面中的验收提醒模块
	 * @author 彭定旭
	 * @Create Date:2013-3-27(创建日期)
	 * @return
	 * @throws Exception
	 */
	public String alermYS() throws Exception
	{
		return "alermYS";
		
	}
	
	

	
}
